package renguanyu.app.auth4fx.view.role;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import renguanyu.app.auth4fx.App;
import renguanyu.app.auth4fx.controller.role.RolePermissionListDeleteController;
import renguanyu.app.auth4fx.controller.role.RolePermissionListQueryController;
import renguanyu.app.auth4fx.controller.role.RolePermissionListSaveController;

public class RolePermissionListView extends VBox {

    public RoleInfoUpdateView roleInfoUpdateView;
    public RolePermissionListView(RoleInfoUpdateView roleInfoUpdateView) {
        this.roleInfoUpdateView = roleInfoUpdateView;

        HBox row1 = new HBox(10);
        VBox leftPanel = createLeftPanel(); // 创建左侧面板
        VBox rightPanel = createRightPanel(); // 创建右侧面板
        row1.getChildren().addAll(leftPanel, rightPanel); // 组装
//        row1.setPadding(new Insets(10));
        VBox.setVgrow(row1,Priority.ALWAYS);

        HBox row2 = new HBox(10);
        Button backButton = new Button("返回");
        backButton.setOnAction(event -> {
            App.mainView.setCenter(new RoleInfoUpdateView(roleInfoUpdateView.roleListView));
        });
        row2.getChildren().addAll(backButton);
//        row2.setPadding(new Insets(0,10,10,10));
        row2.setAlignment(Pos.TOP_CENTER);

        getChildren().addAll(row1,row2);
        setSpacing(10);
        setPadding(new Insets(15));

        new RolePermissionListQueryController(this).execute();// 初始化时加载数据
    }

    public RolePermissionListView() {
        HBox row1 = new HBox(10);
        VBox leftPanel = createLeftPanel(); // 创建左侧面板
        VBox rightPanel = createRightPanel(); // 创建右侧面板
        row1.getChildren().addAll(leftPanel, rightPanel); // 组装
        VBox.setVgrow(row1,Priority.ALWAYS);

        HBox row2 = new HBox(10);
        Button backButton = new Button("返回");
        backButton.setOnAction(event -> {
            App.mainView.setCenter(new RoleListView());
        });
        row2.getChildren().addAll(backButton);
        row2.setAlignment(Pos.TOP_CENTER);

        getChildren().addAll(row1,row2);
        setSpacing(10);
        setPadding(new Insets(15));

        new RolePermissionListQueryController(this).execute();// 初始化时加载数据
    }

    public VBox leftCheckBoxContainer;
    private VBox createLeftPanel() {
        VBox leftPanel = new VBox(10);
        HBox.setHgrow(leftPanel, Priority.ALWAYS);

        Button addPermissionBtn = new Button("添加权限");
        addPermissionBtn.setOnAction(event -> {
            new RolePermissionListSaveController(this).execute();
        });

        Label unownedPermissionLabel = new Label("未拥有的权限");

        ScrollPane leftScrollPane = new ScrollPane();
        leftScrollPane.setPadding(new Insets(10));

        leftCheckBoxContainer = new VBox(10);

        leftScrollPane.setContent(leftCheckBoxContainer);
        leftPanel.getChildren().addAll(addPermissionBtn, unownedPermissionLabel, leftScrollPane);
        return leftPanel;
    }

    public VBox rightCheckBoxContainer;
    private VBox createRightPanel() {
        VBox rightPanel = new VBox(10);
        HBox.setHgrow(rightPanel,Priority.ALWAYS);

        Button deletePermissionBtn = new Button("删除权限");
        deletePermissionBtn.setOnAction(event -> {
            new RolePermissionListDeleteController(this).execute();

        });

        Label ownedPermissionLabel = new Label("已拥有的权限");

        ScrollPane rightScrollPane = new ScrollPane();
        rightScrollPane.setPadding(new Insets(10));

        rightCheckBoxContainer = new VBox(10);

        rightScrollPane.setContent(rightCheckBoxContainer);
        rightPanel.getChildren().addAll(deletePermissionBtn, ownedPermissionLabel, rightScrollPane);
        return rightPanel;
    }
}